<template>
  <div>
    <!-- 子组件通知父组件关闭对话框 -->
    <van-nav-bar
      title="设置昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onClickRight"
    />
    <div class="box">
      <van-field
        v-model="localName"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { updateUserName } from '@/api/user'

export default {
  name: 'upDateName',
  props: {
    value: String
  },
  data () {
    return {
      localName: this.value
    }
  },
  methods: {
    async onClickRight () {
      try {
        // 发请求
        await updateUserName({ name: this.localName })
        //   提示成功
        this.$toast.success('操作成功')
        //   更新父组件数据
        this.$emit('input', this.localName)
        // 关闭弹层
        this.$emit('close')
      } catch (e) {
        console.log(e)
        this.$toast(e.message)
      }
    }
  }

}
</script>

<style lang="less" scoped>
.box {
  padding: 20px;
}
</style>